package com.cloud.pole.advert.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.advert.biz.AdvertBiz;
import com.cloud.pole.advert.biz.AdvertPosBiz;
import com.cloud.pole.advert.model.entity.Advert;
import com.cloud.pole.advert.model.entity.AdvertPos;
import com.cloud.pole.advert.service.AdvertService;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.util.UUIDUtils;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年1月30日
*cloud-user
*author:frank-chen
*/
@Service
@Slf4j
public class AdvertServiceImpl implements AdvertService {
    @Autowired
    private AdvertBiz advertBiz;
    
    @Autowired
    private AdvertPosBiz advertPosBiz;
    
	@Override
	public List<Advert> getAdvertList(Integer advertPosId) throws Exception{
		// TODO Auto-generated method stub
		AdvertPos advertPos = advertPosBiz.selectByPrimaryKey(advertPosId);
		if(advertPos == null || advertPos.getPosStatus()  != 1) {
    		throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "advertPosId错误");
		}
		return advertBiz.selectListByAdvertPosId(advertPosId);
	}
	
	@Override
	public List<Advert> getAdvertListByShopId(Integer shopId, Integer advertPosId)  throws Exception{
		// TODO Auto-generated method stub
		AdvertPos advertPos = advertPosBiz.selectByPrimaryKey(advertPosId);
		if(advertPos == null || advertPos.getPosStatus()  != 1) {
    		throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "advertPosId错误");
		}
		return advertBiz.selectListByShopId(shopId, advertPosId);
	}

	@Override
	public void modifyShopAdvertList(Integer shopId, Integer merchantId, Integer advertPosId, List<String> advertList) throws Exception {
		// TODO Auto-generated method stub
		//删除
		advertBiz.deleteByShopId(shopId, advertPosId);
		//增加
		for(String imagePath : advertList) {
			Advert record = new Advert();
			record.setShopId(shopId);
			record.setMerchantId(merchantId);
			record.setImagePath(imagePath);
			record.setAdvertPosId(advertPosId);
			record.setCreateTime(new Date());
			advertBiz.insertSelectiveByShopId(record);
		}
	}

	@Override
	public PageInfo<Advert> selectPageShopGoodsGroupAdminList(Integer pageNo, Integer pageSize,Integer shopId)
			throws Exception {
		PageHelper.startPage(pageNo, pageSize);
		 List<Advert> selectByPage = advertBiz.selectByPage(shopId);
		PageInfo<Advert> page = new PageInfo<Advert>(selectByPage);
		return page;
	}

	@Override
	public void addBanner(String token,Advert advert) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		advert.setCreateTime(new Date());
		advert.setCreateUserId(jwtInfo.getId());
		advert.setAdvertPosId(1);
		advert.setMerchantId(1);
		advertBiz.addOpendBanner(advert);
	}

	@Override
	public Advert selectById(Integer advertId) throws Exception {
		return advertBiz.selectByAdvertId(advertId);
	}

	@Override
	public void updateBanner(Advert advert) throws Exception {
		advertBiz.updateOpendBanner(advert);
		
	}

	@Override
	public void deleteBanner(Integer advertId) throws Exception {
		advertBiz.deleteBanner(advertId);
		
	}
}
